package com.tjetc.impl;

import com.tjetc.dao.ApartmentMapper;
import com.tjetc.domian.Apartment;
import com.tjetc.service.ApartmentService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.math.BigDecimal;
import java.util.List;

public class ApartmentServiceImpl implements ApartmentService {
    private ApartmentMapper apartmentMapper = Db.getMapper(ApartmentMapper.class);

    @Override
    public List<Apartment> list() {
        return apartmentMapper.select();
    }

    @Override
    public Result del(int id) {
        int rows = apartmentMapper.deleteByPrimaryKey(id);
        return rows == 1 ? new Result(200, "删除成功", null) : new Result(500, "删除失败", null);
    }

    @Override
    public Result update(Apartment apartment) {
        if (apartment == null) {
            return new Result(400, "更改内容不能为空", null);
        } else {
            if (apartment.getApartmentid() == null) {
                return new Result(400, "查询不到要更改的公寓", null);
            } else {
                int rows = apartmentMapper.updateByPrimaryKeySelective(apartment);
                return rows == 1 ? new Result(200, "修改成功", apartment) : new Result(500, "修改失败", null);
            }
        }

    }

    @Override
    public Result add(Apartment apartment) {
        int rows = apartmentMapper.insert(apartment);
        return rows == 1 ? new Result(200, "添加成功", apartment) : new Result(500, "添加失败", null);

    }

    @Override
    public List<Apartment> listByArea(BigDecimal area) {
        return apartmentMapper.listByArea(area);
    }
}
